using System;
using System.Collections;
using System.Collections.Generic;
using Main.Event;
using UnityEngine;
using ZSW.Framework;

namespace Main.Light
{
    public class LightManager : ZSWF_InitMonoBehaviour
    {
        private LightControl[] sceneLights;
        private LightShift lightShift;
        private Season currentSeason;
        private float timeDifference;

        public override void Init()
        {
            base.Init();

            MainEventSystem.AfterLoadSceneEvent += OnAfterLoadScene;
            MainEventSystem.LightShiftChangEvent += OnLightShiftChange;
            MainEventSystem.StartNewGameEvent += OnStartNewGame;
        }

        private void OnStartNewGame(int obj)
        {
            lightShift = LightShift.Morning;
        }

        private void OnLightShiftChange(Season season, LightShift shift, float timeDifference)
        {
            currentSeason = season;
            this.timeDifference = timeDifference;
            if (lightShift != shift)
            {
                lightShift = shift;
                if (sceneLights == null)
                {
                    sceneLights = FindObjectsOfType<LightControl>();
                }

                foreach (LightControl light in sceneLights)
                {
                    light.ChangeLight(currentSeason, lightShift, timeDifference);
                }
            }
        }

        private void OnAfterLoadScene()
        {
            sceneLights = FindObjectsOfType<LightControl>();

            foreach (LightControl light in sceneLights)
            {
                light.ChangeLight(currentSeason, lightShift, timeDifference);
            }
        }

        private void OnDisable()
        {

        }
    }
}